﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
namespace LeHouse.Server.WebApi.Controllers
{
    public class FacilityController : ApiController
    {
        Collateral_FacilityService bll = new Collateral_FacilityService();
        /// <summary>
        /// 建筑公共部位状况的数据添加
        /// </summary>
        /// <param name="s">ajax传过来的需要添加的数据</param>
        /// <returns></returns>       
        [HttpPost]
        [Route("api/Facility/PostInsert")]
        public bool Insert([FromBody] Collateral_Facility parms)
        {
            Collateral_PersonalService PerService = new Collateral_PersonalService();
            Collateral_Personal state = PerService.Select(p => p.T_ReportNumber == parms.T_ReportNumber).FirstOrDefault();
            if (state.T_State >=2 && state.T_State != 4)
            {
                return false;
            }
            else
            {
                if (bll.IsCount(parms.T_ReportNumber) > 0)
                {
                    return bll.Update(parms, t => t.T_ReportNumber == parms.T_ReportNumber);
                }
                else
                {
                    return bll.Insert(parms);
                }
            }
          
        }

        /// <summary>
        /// 根据报告号获取建筑公共部位状况的数据
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Facility/SelectByid")]
        public Collateral_Facility showfacilty([FromUri] string P_ReportNumber)
        {
            return bll.Select(p => p.T_ReportNumber == P_ReportNumber).FirstOrDefault();
        }
    }
}
